package com.example.mynetwork2;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.mynetwork2.bean.Test2Bean;
import com.example.mynetwork2.bean.TestBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpActivity extends AppCompatActivity {
    Button download;
    ImageView imageView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
         download = (Button) findViewById(R.id.download_bt);
         imageView = (ImageView) findViewById(R.id.my_image);
        Button load_image = (Button) findViewById(R.id.load_image);
        download.setOnClickListener((v)->{
             download();
         });

        load_image.setOnClickListener((v)->{
            //加载图片
            String url = "http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg";
            // this：上下文   url：要加载的图片链接   imageView: 要显示的图片控件
            Glide.with(this).load(url).into(imageView);
        });




    }

    private void download() {
//        //创建client对象
//        OkHttpClient client = new OkHttpClient();
//        //创建request
//        Request request = new Request.Builder()
//                .url("https://www.baidu.com")
//                .get()
//                .build();
//        //创建call对象
//        Call call = client.newCall(request);

        //get同步请求，但是主线程不能执行耗时操作，所以需要创建一个线程来执行网络请求
//        new Thread(()->{
//            try {
//                Response response = call.execute();
//                String string = response.body().string();
//                Log.d("TAG1",string);
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }).start();
        //get异步请求
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                Log.d("onFailure",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                String content = response.body().string();
//                Log.d("TAG2",content);
//            }
//        });
        //post
        OkHttpClient client = new OkHttpClient();
//        // 上传json
//        String url ="http://10.111.34.9:8080/okhttp/testJson";
//        //构造 RequestBody 需要指定MediaType，用于描述请求/响应 body 的内容类型
//        MediaType typeToJson = MediaType.parse("application/json; charset=utf-8");
//        String jsonData="{\"name\":\"zhangsan\",\"age\":\"20\"}";
//        Request request = new Request.Builder()
//                .url(url)
//                .post(RequestBody.create(typeToJson,jsonData))
//                .build();
//        //创建call
//        Call call = client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                Log.d("onFailure",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                 Log.d("onResponse",response.body().string());
//            }
//        });


//        //构造 RequestBody 需要指定MediaType，用于描述请求/响应 body 的内容类型
//        RequestBody requestBody = new FormBody.Builder()
//                .add("name", "李四")
//                .add("age", "18")
//                .build();
//
//        Request request = new Request.Builder()
//                .url("http://10.111.34.9:8080/okhttp/testForm")
//                .post(requestBody)
//                .build();
//
//        Call call = client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                Log.d("onFailure",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                 Log.d("onResponse",response.body().string());
//            }
//        });

        //json数据解析
        OkHttpClient client1 = new OkHttpClient();
//        Request request = new Request.Builder()
//                .url("http://10.111.34.9:8080/okhttp/hello2")
//                .get()
//                .build();
        //请求复杂的json数据
        Request request2 = new Request.Builder()
                .url("https://www.wanandroid.com/article/list/0/json")
                .get()
                .build();

        Call call = client1.newCall(request2);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                  Log.d("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                //创建gson对象
                Gson gson = new Gson();
//                TestBean testBean = gson.fromJson(json, TestBean.class);
//                Integer code = testBean.getCode();
//                TestBean.DataBean data = testBean.getData();
//                Log.d("GSON","code: "+200+" name: "+data.getName()+" age: "+data.getAge());

                Test2Bean test2Bean = gson.fromJson(json, Test2Bean.class);
                Test2Bean.DataBean data = test2Bean.getData();
                List<Test2Bean.DataBean.DatasBean> datas = data.getDatas();
                for (Test2Bean.DataBean.DatasBean data1 :datas){

                }

            }
        });


    }
}
